
/*

Design Pattern Automation Toolkit.
Application to create applications with emphasis on Design patterns.
And support for round trip engineering.
Copyright (C) 2004 Vineeth Neelakant. nvineeth@gmail.com

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

*/
using System;
using BusinessLayer;
using InfoClasses;
using dpatoolkit.UILayer;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using DesignPatternDefinitions;

namespace dpatoolkit.UILayer
{
	/// <summary>
	/// Represents a UI for multiple reference relation
	/// </summary>
	public class UIMultiReference : IUIRelation
	{
		
		#region Fields
		private static  Pen scPen , ecPen , ncPen;
		#endregion 
		
		#region Constructor
		static UIMultiReference()
		{

            ecPen = new Pen(UIRenderer.MultiReferenceColor, UIRenderer.RelationPenSize);
			ecPen.CustomEndCap=PenCapProvider.GetMultipleRepresentation();
            ncPen = new Pen(UIRenderer.MultiReferenceColor, UIRenderer.RelationPenSize);
		}
		
		public UIMultiReference(string txt , IUIObject _o1, IUIObject _o2,IDesignPatternSubject _sub):
			base ( txt,_o1,_o2,null)
		{
			this.subject = _sub;
			this.relationContextMenu.MenuItems[editIndex].Click +=new EventHandler(UIMultiRefEdit_Click);
			this.relationContextMenu.MenuItems[revIndex].Click +=new EventHandler(UIMultiRefReverse_Click);
			this.relationContextMenu.MenuItems[delIndex].Click +=new EventHandler(UIMultiRefDel_Click);
            this.relationType = RelationType.MultiReference;
		}	

		#endregion

		#region IUIRelation Members

		
		public  override void Draw(Graphics g)
		{
			base.DrawLines(g,ncPen,ecPen,ncPen);
	
		}

		public  override void SetPariticapants(Rectangle r1, Rectangle r2)
		{
			
		}

		
		public override ContextMenu ContextMenu
		{
			get
			{
				return this.relationContextMenu;
			}
			set
			{
				//not implemented :P :-P
			}
		}




		#endregion


		private IReference ConstructRelationObject()
		{
			IReference _ref = new ReferenceInfo();
			_ref.ReferenceClass = this.obj1.UIName;
			_ref.ReferenceComponent = this.obj1.UIComponent;
			_ref.ReferenceName = this.RelationName;
			_ref.ReferedClassName = this.obj2.UIName;
			_ref.ReferedComponentName = this.obj2.UIComponent;
			_ref.Order = RelationOrder.Multiple;
			return _ref;
		}
		private void UIMultiRefEdit_Click(object sender, EventArgs e)
		{
			IReference oldref = this.ConstructRelationObject();
			IReference newref = this.ConstructRelationObject();
			RelationEditForm form = new RelationEditForm("Edit Reference");
			form.Input= oldref.ReferenceName;
			form.Order = oldref.Order;
			if( form.ShowDialog()== DialogResult.Cancel)
				return;
			newref.ReferenceName= form.Input;
			newref.Order = form.Order;
			if(oldref.Order!=newref.Order || newref.ReferenceName!=oldref.ReferenceName)
				this.subject.UpdateReference(oldref,newref);


		}
		private void UIMultiRefDel_Click(object sender, EventArgs e)
		{
			IReference _ref = this.ConstructRelationObject();
			this.UnRegisterObserver();
			this.subject.RemoveReference(_ref);

		}

		private void UIMultiRefReverse_Click(object sender, EventArgs e)
		{
			IReference oldref = this.ConstructRelationObject();
			if(oldref.ReferenceComponent!=oldref.ReferedComponentName)
			{
				InfoMessage.Show("Sorry currently not supported when exported classes are present!");
				return;
			}

			IReference newref = this.ConstructRelationObject();
			newref.ReferedClassName = oldref.ReferenceClass;
			newref.ReferenceClass = oldref.ReferedClassName;
			newref.ReferenceComponent = oldref.ReferedComponentName;
			newref.ReferedComponentName = oldref.ReferenceComponent;
			this.subject.UpdateReference(oldref,newref);
				
		}

	}
}
